home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 August: Technology Seed / ADC Seed CD - August 1999.toast / Carbon SDK 1.0d10c3 / Sample Code / AppearanceSample / UtilityWindow.cp < prev    next >
Encoding:
Text File  |  1999-05-01  |  7.7 KB  |  278 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        UtilityWindow.cp
  3.  
  4.     Contains:    Utility window tester class.
  5.  
  6.     Version:    Appearance 1.0 SDK
  7.  
  8.     Copyright:    © 1997, 1999 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     File Ownership:
  11.  
  12.         DRI:                Edward Voas
  13.  
  14.         Other Contact:        7 of 9, Borg Collective
  15.  
  16.         Technology:            OS Technologies Group
  17.  
  18.     Writers:
  19.  
  20.         (jss)    Jeff Shulman
  21.         (edv)    Ed Voas
  22.  
  23.     Change History (most recent first):
  24.  
  25.        <1.7>      4/7/99    jss        Fix CarbonLib compilation problems
  26.          <1>     9/11/97    edv        First checked in.
  27. */
  28.  
  29. #include "AppearanceSamplePrefix.h"
  30.  
  31. #include <Appearance.h>
  32. #include <ControlDefinitions.h>
  33. #include <MacWindows.h>
  34. #include "Assertions.h"
  35. #include "UtilityWindow.h"
  36.  
  37. #define width( r )        ( (r).right - (r).left )
  38. #define height( r )        ( (r).bottom - (r).top )
  39.  
  40. UtilityWindow::UtilityWindow( SInt16 windID ) : BaseWindow( windID )
  41. {                
  42.     Rect        rect;
  43.     
  44.         // Note the use of the new defProc constants here.
  45.         // This eliminates the need to go thru the mapping
  46.         // CDEF for scroll bars, which would normally happen
  47.         // after calling RegisterAppearanceClient.
  48.         
  49.     CalcVertScrollBarRect( rect );
  50.     CreateScrollBarControl( fWindow, &rect, 0, 0, 100, 0, false, nil, &fVertScrollBar );
  51.     
  52.     CalcHorizScrollBarRect( rect );
  53.     CreateScrollBarControl( fWindow, &rect, 0, 0, 100, 0, false, nil, &fHorizScrollBar );
  54. }
  55.  
  56. UtilityWindow::UtilityWindow() : BaseWindow( 131 )
  57. {                
  58.     Rect        rect;
  59.     
  60.         // Note the use of the new defProc constants here.
  61.         // This eliminates the need to go thru the mapping
  62.         // CDEF for scroll bars, which would normally happen
  63.         // after calling RegisterAppearanceClient.
  64.         
  65.     CalcVertScrollBarRect( rect );
  66.     CreateScrollBarControl( fWindow, &rect, 0, 0, 100, 0, false, nil, &fVertScrollBar );
  67.     
  68.     CalcHorizScrollBarRect( rect );
  69.     CreateScrollBarControl( fWindow, &rect, 0, 0, 100, 0, false, nil, &fHorizScrollBar );
  70. }
  71.  
  72. UtilityWindow::~UtilityWindow()
  73. {
  74.     if ( fVertScrollBar )
  75.         DisposeControl( fVertScrollBar );
  76.     
  77.     if ( fHorizScrollBar )
  78.         DisposeControl( fHorizScrollBar );
  79. }
  80.  
  81. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  82. //    Ä Activate
  83. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  84. //    Activates the contents of the window.
  85. //
  86. void
  87. UtilityWindow::Activate( EventRecord& )
  88. {
  89.         // Here we use the new ActivateControl call. We
  90.         // could have still used HiliteControl, but this
  91.         // is much more straightforward. It also works
  92.         // right with and without embedding, so it's a
  93.         // real good idea to start using it.
  94.         
  95.     ActivateControl( fHorizScrollBar );
  96.     ActivateControl( fVertScrollBar );
  97. }
  98.  
  99. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  100. //    Ä Deactivate
  101. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  102. //    Deactivates the contents of the window.
  103. //
  104. void
  105. UtilityWindow::Deactivate( EventRecord& )
  106. {
  107.         // Here we use the new DeactivateControl call. We
  108.         // could have still used HiliteControl, but this
  109.         // is much more straightforward. It also works
  110.         // right with and without embedding, so it's a
  111.         // real good idea to start using it.
  112.         
  113.     DeactivateControl( fHorizScrollBar );
  114.     DeactivateControl( fVertScrollBar );
  115. }
  116.  
  117. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  118. //    Ä Draw
  119. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  120. //    Draws our window.
  121. //
  122. void
  123. UtilityWindow::Draw()
  124. {
  125.     RgnHandle    region = NewRgn();
  126.  
  127.     if ( region )
  128.     {
  129.         UpdateControls( fWindow, GetPortVisibleRegion( fPort, region ) );
  130.         DisposeRgn( region );
  131.     }
  132. }
  133.  
  134. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  135. //    Ä Resize
  136. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  137. //    Resize our window to the appropriate size specified in width and height. Make sure
  138. //    the scroll bars are repositioned properly.
  139. //
  140. void
  141. UtilityWindow::Resize( SInt16 width, SInt16 height )
  142. {
  143.     Rect        horizRect, vertRect;
  144.     
  145.     InvalidateScrollBars();
  146.     
  147.     BaseWindow::Resize( width, height );
  148.     
  149.     InvalidateScrollBars();
  150.     
  151.     CalcHorizScrollBarRect( horizRect );
  152.     CalcVertScrollBarRect( vertRect );
  153.     
  154.     MoveControl( fHorizScrollBar, horizRect.left, horizRect.top );
  155.     MoveControl( fVertScrollBar, vertRect.left, vertRect.top );
  156.     SizeControl( fHorizScrollBar, width( horizRect ), height( horizRect ) );
  157.     SizeControl( fVertScrollBar, width( vertRect ), height( vertRect ) );
  158. }
  159.  
  160. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  161. //    Ä HandleClick
  162. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  163. //    Simple routine to handle scroll bar tracking, even though they don't do anything.
  164. //
  165. void
  166. UtilityWindow::HandleClick( EventRecord& event )
  167. {
  168.     ControlHandle        control;
  169.     SInt16                part;
  170.     Point                localPt;
  171.     ControlActionUPP    actionProc;
  172.     
  173.     SetPort( fPort );
  174.     localPt = event.where;
  175.     GlobalToLocal( &localPt );
  176.     
  177.     part = FindControl( localPt, fWindow, &control );
  178.     switch ( part )
  179.     {
  180.         case kControlUpButtonPart:
  181.         case kControlDownButtonPart:
  182.         case kControlPageUpPart:
  183.         case kControlPageDownPart:
  184.             actionProc = NewControlActionProc( ScrollBarAction );
  185.             TrackControl( control, localPt, actionProc );
  186.             DisposeControlActionUPP( actionProc );
  187.             break;
  188.         
  189.         case kControlIndicatorPart:
  190.             TrackControl( control, localPt, (ControlActionUPP)-1L );
  191.             break;
  192.     }
  193. }
  194.  
  195. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  196. //    Ä InvalidateScrollBars
  197. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  198. //    Invalidates the scroll bar areas.
  199. //
  200. void
  201. UtilityWindow::InvalidateScrollBars()
  202. {
  203.     Rect        tempRect;
  204.     
  205.     SetPort( fPort );
  206.     CalcHorizScrollBarRect( tempRect );
  207.     InvalWindowRect( fWindow, &tempRect );
  208.     EraseRect( &tempRect );
  209.     
  210.     CalcVertScrollBarRect( tempRect );
  211.     InvalWindowRect( fWindow, &tempRect );
  212.     EraseRect( &tempRect );
  213. }
  214.  
  215. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  216. //    Ä CalcHorizScrollBarRect
  217. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  218. //    Calculates the position where the horizontal scroll bar should be placed.
  219. //
  220. void
  221. UtilityWindow::CalcHorizScrollBarRect( Rect& rect )
  222. {
  223.     GetPortBounds( fPort, &rect );
  224.     rect.bottom++;
  225.     rect.left = -1;
  226.     rect.top = rect.bottom - 11;
  227.     rect.right -= 9;
  228. }
  229.  
  230. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  231. //    Ä CalcVertScrollBarRect
  232. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  233. //    Calculates the position where the vertical scroll bar should be placed.
  234. //
  235. void
  236. UtilityWindow::CalcVertScrollBarRect( Rect& rect )
  237. {
  238.     GetPortBounds( fPort, &rect );
  239.     rect.right++;
  240.     rect.left = rect.right - 11;
  241.     rect.bottom -= 9;
  242.     rect.top = -1;
  243. }
  244.  
  245. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  246. //    Ä ScrollBarAction
  247. //ãããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããããã
  248. //    A simple callback to give some feedback when clicking the scroll arrows or page
  249. //    up/down areas.
  250. //
  251. pascal void
  252. UtilityWindow::ScrollBarAction( ControlHandle control, SInt16 part )
  253. {
  254.     switch ( part )
  255.     {
  256.         case kControlUpButtonPart:
  257.             if ( GetControlValue( control) > GetControlMinimum( control ) )
  258.                 SetControlValue( control, GetControlValue( control ) - 1 );
  259.             break;
  260.         
  261.         case kControlDownButtonPart:
  262.             if ( GetControlValue( control) < GetControlMaximum( control ) )
  263.                 SetControlValue( control, GetControlValue( control ) + 1 );
  264.             break;
  265.             
  266.         case kControlPageUpPart:
  267.             if ( GetControlValue( control) > GetControlMinimum( control ) )
  268.                 SetControlValue( control, GetControlValue( control ) - 10 );
  269.             break;
  270.             
  271.         case kControlPageDownPart:
  272.             if ( GetControlValue( control) < GetControlMaximum( control ) )
  273.                 SetControlValue( control, GetControlValue( control ) + 10 );
  274.             break;
  275.     }            
  276. }
  277.  
  278.